/**
 * 
 */
package pt.mleiria.cipher;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Logger;

import pt.mleiria.bit.BytesConverter;

/**
 * @author NB13712
 *
 */
public class Tea {
	private static final Logger LOG = Logger.getLogger(Tea.class);

	// 64-bit block to encrypt
	private final String text = "0123456789ABCDEF";
	private final String delta = "9E3779B9";
	
	//final String keyString = "A56BABCD00000000FFFFFFFFABCDEF01";
	private int[] k = new int[4];; 
	private byte[] plainText;
	private final int[] l = new int[4];
	private final int[] r = new int[4];
	
	/**
	 * @param keyString, a 128-bit key
	 * @throws DecoderException 
	 * 
	 */
	public Tea(final String keyString) throws Exception {
		byte[] key = Hex.decodeHex(keyString.toCharArray());
		//key = keyString.getBytes("UTF-8");
		for(int i = 0, offSet = 0; i < k.length; i++){
			k[i] = (key[offSet++] & 0xff) | ((key[offSet++] & 0xff) << 8) | ((key[offSet++] & 0xff) << 16) | ((key[offSet++] & 0xff) << 24); 
			//LOG.info(BitWiseUtils.fromHexadecimalToBinary(k[i]));
		}
		splitText();
	}
	private void splitText() throws Exception{
		plainText = Hex.decodeHex(text.toCharArray());
		int paddedSize = ((plainText.length/8) + (((plainText.length%8)==0)?0:1)) * 2;
		LOG.info(paddedSize);
		LOG.info((plainText.length/8));
		LOG.info(plainText.length%8);
		
		for(int i = 0; i < plainText.length; i++)
			LOG.info(BytesConverter.fromHexadecimalToBinary(plainText[i]));
		
	}
	
	
}
